<?php
function deleteMessage($id){
    global $bdd;

    $req = $bdd->prepare('DELETE FROM contact WHERE ID=?');
    $req->execute(array($id)) or die(print_r($req->errorInfo()));
    $req->closeCursor();
}

function envoiMessage($nom, $mail, $site, $message, $note)
{
    global $bdd;
    $req = $bdd->prepare('INSERT INTO contact (nom, mail, website, message)VALUES (:nom, :mail, :website, :message)');
    $req->execute(array('nom' => $nom, 'mail' => $mail, 'website' => $site, 'message' => $message)) or die(print_r($req->errorInfo()));
    $req->closeCursor();
    $req = $bdd->prepare('INSERT INTO note VALUES (?)');
    $req->execute(array($note)) or die(print_r($req->errorInfo()));
    $req->closeCursor();
    return 1;
}

function getMessage($id){
    global $bdd;

    $req = $bdd->prepare('SELECT * FROM contact WHERE ID = ? LIMIT 0,1');
    $req->execute(array($id)) or die(print_r($req->errorInfo()));
    $result = $req->fetchAll();
    return $result;
}
function getMessages($debut, $limit)
{
    global $bdd;
    $req = $bdd->query('SELECT * FROM contact ORDER BY ID LIMIT '.($debut).','.$limit) or die(print_r($bdd->errorInfo()));
    $message = $req->fetchAll();
    $req->closeCursor();
    return $message;
}

function listContact(){
    global $bdd;

    $req = $bdd->query('SELECT * FROM contact ORDER BY ID') or die(print_r($bdd->errorInfo()));
    $result = $req->fetchAll();
    return $result;
}

function getNombreContact(){
    global $bdd;
    $req = $bdd->query('SELECT COUNT(*) AS nb FROM `contact`') or die(print_r($bdd->errorInfo()));
    $result = $req->fetch();
    $nbContact = $result['nb'];
    $req->closeCursor();
    return $nbContact;
}

function selectInfosMembre(){
// Si le membre est connecté, on va récupérer ses infos pour remplir automatiquement les champs du formulaire
    global $bdd;
    if (isset($_SESSION['ID'])) {
        $req = $bdd->prepare('SELECT * FROM membres WHERE ID = ? ');
        $req->execute(array($_SESSION['ID'])) or die(print_r($req->errorInfo()));
        $membre = $req->fetchAll();
        $req->closeCursor();
        return $membre;
    }
    return array('');
}